package Client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import GUI.AddScenario;
import GUI.Bank;
import GUI.Center;
import GUI.FullRun;
import GUI.MainScreen;
import GUI.Menu;
import GUI.Run;
import Logic.LoginInfo;
import ocsf.client.AbstractClient;

/**
 * this class get answer from the server 
 * @author Gabi Revah
 */

public class ClientHendler extends AbstractClient {
	public static Object togui;
	public static ArrayList<Object> msg;
	
	  
	public ClientHendler(String host, int port) throws IOException {
		super(host, port);
		openConnection();
		
		
	}


	@SuppressWarnings("unchecked")
	protected void handleMessageFromServer(Object msg) {
		
		
		this.msg=(ArrayList<Object>) msg;
		System.out.println("handleMessageFromServer:");
	
		
		
			 if(msg.toString().contains("LogoutCompleted"))
			{
				MainScreen.cardLayot.show(MainScreen.card, "Login");
				JOptionPane.showMessageDialog(null, "You are successfully logout from HTSCenter");

			}
			 
			else if(msg.toString().contains("LogoutProblem"))
			{
				
				JOptionPane.showMessageDialog(null, "Logout Failed");

			}
			else if(msg.toString().contains("AddUser"))
			{
				
				JOptionPane.showMessageDialog(null, "User was added successfully to the HTS Center");
				MainScreen.cardLayot.show(MainScreen.card, "Center");

			}
			
			
			else if(msg.toString().contains("NotInTheSystem"))
			{		
				JOptionPane.showMessageDialog(null, "The user name or password provided is incorrect.");
			}
			else if(msg.toString().contains("StatusRun"))
			{		
				JOptionPane.showMessageDialog(null, "The scenario is already in Run ");
			}
			
			 
			else if(msg.toString().contains("MultipleConnection"))
			{		
				JOptionPane.showMessageDialog(null, " You already connected to the system! ");
			}
			else if(msg.toString().contains("Error"))
			{		
				JOptionPane.showMessageDialog(null, " General Error! ");
			}
			
	

		
		
		else if(((ArrayList<Object>) msg).get(0).equals("Bank")){
			 
			//String[] str;
			
			//msg= msg.toString().substring(1, msg.toString().length()-1); // remove the [] from the msg by substring 
			//str = msg.toString().split("[ , ] "); // remove the , from msg and spliting it to tiny str array parts  

		 	
		 	Bank b = new Bank((ArrayList<Object>) msg);			
			MainScreen.card.add("Bank", b);
			MainScreen.cardLayot.show(MainScreen.card, "Bank");
	 }
		
		else if(((ArrayList<Object>) msg).get(0).equals("RunStepFirst")){
			 
			

		 	
		 	Run b = new Run((ArrayList<Object>) msg);			
			MainScreen.card.add("Run", b);
			MainScreen.cardLayot.show(MainScreen.card, "Run");
	 }
		
		else if(((ArrayList<Object>) msg).get(0).equals("LoadStep")){
		 	
			FullRun bs = new FullRun((ArrayList<Object>) msg);			
			MainScreen.card.add("FullRun", bs);
			MainScreen.cardLayot.show(MainScreen.card, "FullRun");
	 }
		
		else if(((ArrayList<Object>) msg).get(0).equals("AddScenario")){
			 
			
			Menu.AddScenarioFrame.setVisible(false);
			
			AddScenario as = new AddScenario((ArrayList<Object>) msg);			
			MainScreen.card.add("AddScenario", as);
			MainScreen.cardLayot.show(MainScreen.card, "AddScenario");
	 }
		
		
		
		else if(((ArrayList<Object>) msg).get(0).equals("Center")){
			 
			
			
			MainScreen.cardLayot.show(MainScreen.card, "Center");
	 }
		
		else if(this.msg.get(0).equals("Manager")){
			 
			
			
			LoginInfo.setRoll("0");
			Center c = new Center((ArrayList<Object>) msg);
			MainScreen.card.add("Center", c);
			MainScreen.cardLayot.show(MainScreen.card, "Center");
			
		
	 }
		
	
		
		
		
		
	}//End of handle message of server
		
		

	  public void handleMessageFromGui(Object msg)
	  {
	    try
	    {
	    	sendToServer(msg);
	    }
	    catch(IOException e)
	    {
	 
	       System.out.println("Could not send message to server.  Terminating client.");
	      e.printStackTrace();
	      quit();
	    }
	  }
	  
	  /**
	   * This method terminates the client.
	   */
	  public void quit()
	  {
	    try
	    {
	      closeConnection();
	    }
	    catch(IOException e) {}
	    System.exit(0);
	  }


	
	}
	//End of ChatClient class

	
	
	
	